package com.gxpt.app.view;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gxpt.app.BaseActivity;
import com.gxpt.app.Constant;
import com.gxpt.app.MessageEvent;
import com.gxpt.app.R;
import com.utils.T;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_invoice)
public class Activity_invoice extends BaseActivity {

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.isChecked)
    CheckBox isChecked;

    @ViewInject(R.id.toedit)
    RelativeLayout toedit;

    @ViewInject(R.id.toNext)
    Button toNext;


    @ViewInject(R.id.txt_gone)
    TextView txt_gone;

    Bundle d = null;


    int invoice_type = 0 ;//发票类型 0.个人 1.企业
    int invoice_desc = 0 ;// 0明细 1耗材 2办公用品3 电脑配件

    String invoice_title = null;
    String invoice_phone = null;
    String invoice_email = null;

    int has_invoice = 0 ;//0 不开 1 开具

    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MessageEvent messageEvent){
        if(messageEvent.getMessageType() ==Constant.ACTION_invoice_info ){

            invoice_type = messageEvent.getInt_1();
            invoice_desc = messageEvent.getInt_2();

            invoice_title = messageEvent.getStr_1();
            invoice_phone = messageEvent.getStr_2();
            invoice_email = messageEvent.getStr_3();

            txt_gone.setText("                     "+invoice_type+" "+invoice_desc + " "+invoice_title+" "+invoice_phone+" "+invoice_email);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        x.view().inject(this);

        d            = getIntent().getBundleExtra("DATA");
        if(d!=null){
            has_invoice = d.getInt("has_invoice");

            invoice_type = d.getInt("invoice_type");
            invoice_desc = d.getInt("invoice_desc");
            invoice_title = d.getString("invoice_title");
            invoice_phone = d.getString("invoice_phone");
            invoice_email = d.getString("invoice_email");
        }
        initView();
    }

    @Override
    public void initView() {
        txt_title.setText(getResources().getString(R.string.txt_xuanzefapiao));
        if(has_invoice == 0 ){
            isChecked.setChecked(true);
        }else{
            isChecked.setChecked(false);
        }
        isChecked.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    has_invoice =0;
                }else{
                    has_invoice =1;
                }
            }
        });
    }

    @Override
    public void initData() {

    }


    @Event(value={R.id.toback,R.id.toNext,R.id.toedit})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                closeActivity();
                break;
            case R.id.toedit:
                Bundle b = new Bundle();
                b.putInt("invoice_type",invoice_type);
                b.putInt("invoice_desc",invoice_desc);
                b.putString("invoice_title",invoice_title);
                b.putString("invoice_phone",invoice_phone);
                b.putString("invoice_email",invoice_email);
                jumpActivity(Activity_invoice_info.class,b,false);
                break;
            case R.id.toNext:
                if(has_invoice == 0){
                    EventBus.getDefault().post(new MessageEvent(Constant.ACTION_invoice_info2,
                            0,
                            0,
                            0,
                            "",
                            "",
                            ""
                    ));
                    closeActivity();
                }else{
                    if(invoice_phone ==null){
                        T.show(context, getResources().getString(R.string.alert_putisnotnull), 1);
                    }else{
                        EventBus.getDefault().post(new MessageEvent(Constant.ACTION_invoice_info2,
                                invoice_type,
                                invoice_desc,
                                has_invoice,
                                invoice_title,
                                invoice_phone,
                                invoice_email
                        ));
                        closeActivity();
                    }

                }

                break;
        }
    }

}
